package com.itheima.jobs;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyun.oss.OSSClient;
import com.itheima.constant.OSSClientConstants;
import com.itheima.constant.RedisConstants;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.AliyunOSSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class ClearImgJob {

    //注入Jedis连接池
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderSettingService orderSettingService;

    //清理图片
    public void clearImg() {
        System.out.println("clearImg()...");
        //1.计算set的差值
        Set<String> set = jedisPool.getResource().sdiff(RedisConstants.SETMEAL_PIC_RESOURCES, RedisConstants.SETMEAL_PIC_DB_RESOURCES);
        //2.遍历集合
        for (String pic : set) {
            //3.删除
            //3.1 阿里云
            OSSClient ossClient = AliyunOSSClientUtil.getOSSClient();
            AliyunOSSClientUtil.deleteFile(ossClient, OSSClientConstants.BACKET_NAME, pic);
            //3.2删除redis集合里面的
            jedisPool.getResource().srem(RedisConstants.SETMEAL_PIC_RESOURCES, pic);
        }
    }

    public void delete() throws Exception {
        System.out.println("清理本月之前的预约数据");
              orderSettingService.delete();

        }


}
